#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common import util
from common.global_field import Model


class GsxtZheJiangWorker(GsxtBaseWorker):
    def get_search_result_html(self, keyword, session):
        param_list = []
        try:
            url = 'http://{host}/client/entsearch/toEntSearch'.format(host=self.host)
            content = self.get_captcha_geetest(url, 'input.se-box', '#popup-submit', keyword, 'div.mod.enterprise-info',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            item_list = jq.find('div.mod.enterprise-info').find('.enterprise-info-list').find('a').items()
            for item in item_list:
                href = item.attr('href')
                if href is None or href == '':
                    continue
                param_list.append({'href': href})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_NOTHING_FIND

    def __get_company_name(self, text):
        try:
            result = PyQuery(text, parser='html').find('span[class="fl mr5"]').eq(0).text()
            return result
        except Exception as e:
            self.log.exception(e)

        return None

    def get_year_info_list(self, text):
        jq = PyQuery(text, parser='html')
        item_list = jq.find("#yearreportTable").find('tr').items()
        for item in item_list:
            try:
                if item.text().find('年度报告') == -1:
                    continue
                year_info = item.find('td').eq(1)
                if year_info is None or year_info == '':
                    continue

                year_list = re.findall('(\d+)', year_info.text())
                year = str(year_list[0]) if len(year_list) > 0 else None
                if year is None:
                    continue

                href = item.find('a').attr('href')
                if href is None or href == '':
                    continue

                yield year, 'http://{host}{href}'.format(host=self.host, href=href)
            except Exception as e:
                self.log.exception(e)

    # 基本信息
    def get_base_info(self, session, url):
        # 获取基础信息
        base_info = self.task_request(session.get, url, proxy_type=1)
        if base_info is None:
            self.log.error('获取基础信息页面失败...')
            return None
        return base_info.text

    # 主要人员
    def get_key_person_info(self, session, url, data, totol_data):
        r = self.task_request(session.post, url=url, data=data, proxy_type=1)
        if r is None:
            return False
        self.append_model(totol_data, Model.key_person_info, url, r.text, post_data=data)
        return True

    # 变更信息
    def get_change_info(self, session, url, data, totol_data):
        r = self.task_request(session.post, url=url, data=data, proxy_type=1)
        if r is None:
            return False
        self.append_model(totol_data, Model.change_info, url, r.text, post_data=data)
        return True

    def get_annual_info(self, session, text, data, totol_data):
        # 暂时不处理 跟解析沟通一下
        for year, url in self.get_year_info_list(text):
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(totol_data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_shareholder_info(self, session, url, data, totol_data):
        try:
            r = self.task_request(session.post, url=url, data=data, proxy_type=1)
            if r is None:
                return False
            self.append_model(totol_data, Model.shareholder_info, url, r.text, post_data=data)
        except Exception as e:
            self.log.exception(e)
            return False
        return True

    def get_branch_info(self, session, url, data, totol_data):
        r = self.task_request(session.post, url=url, data=data, proxy_type=1)
        if r is None:
            return False
        self.append_model(totol_data, Model.branch_info, url, r.text, post_data=data)
        return True

    def get_detail_html_list(self, seed, session, param_list):
        # 保存企业名称
        data_list = []
        rank = 0
        for item in param_list:
            try:
                url = item.get('url', None)
                if url is None:
                    self.log.error('参数存储异常: item = {item}'.format(item=item))
                    continue

                session.headers = {
                    "Host": "gsxt.zjaic.gov.cn",
                    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:50.0) Gecko/20100101 Firefox/50.0",
                    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                    "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
                    "Accept-Encoding": "gzip, deflate",
                    "Connection": "keep-alive",
                    "Referer": "http://{host}/client/entsearch/list?isOpanomaly=&pubType=1&searchKeyWord=27E3EDE988A5466C".format(
                        host=self.host),
                }
                # 基本信息
                base_text = self.get_base_info(session, url)
                if base_text is None:
                    continue
                pri_pid = str(base_text).split('="priPID" value="')[1].split('"/')[0]
                # 获得公司名称
                company_name = self.__get_company_name(base_text)
                if company_name is None or company_name == '':
                    self.log.error('公司名称解析失败..item = {item} {text}'.format(
                        text=base_text, item=item))
                    continue
                # 建立数据模型
                rank += 1
                data = self.get_model(company_name, rank, seed, self.province)
                # yearreport_url = 'http://{host}/entinfo/list.json?_t={rand}'.format(
                #    host=self.host, rand=util.get_time_stamp())
                # yearreport_data = {
                #     'params[priPID]': pri_pid
                # }
                member_url = 'http://{host}/midmember/list.json?_t={rand}'.format(
                    host=self.host, rand=util.get_time_stamp())
                member_data = {
                    'priPID': pri_pid,
                }
                branch_url = 'http://{host}/midbranch/list.json?_t={rand}'.format(
                    host=self.host, rand=util.get_time_stamp())
                branch_data = {
                    'priPID': pri_pid,
                }
                investor_url = 'http://{host}/midinv/list.json?_t={rand}'.format(
                    host=self.host, rand=util.get_time_stamp())
                investor_data = {
                    'params[priPID]': pri_pid,
                    'start': '0',
                    'length': '1000'
                }
                change_url = 'http://{host}/midaltitem/list.json?_t={rand}'.format(
                    host=self.host, rand=util.get_time_stamp())
                change_data = {
                    'params[priPID]': pri_pid,
                    'start': '0',
                    'length': '1000'
                }
                # 存储数据
                self.append_model(data, Model.base_info, url, base_text)
                # 主要人员信息
                if not self.get_key_person_info(session, member_url, member_data, data):
                    self.log.warn('主要人员抓取失败....pripid = {pripid}'.format(pripid=pri_pid))
                    continue
                # 分支机构
                if not self.get_branch_info(session, branch_url, branch_data, data):
                    self.log.warn('分支机构抓取失败....pripid = {pripid}'.format(pripid=pri_pid))
                    continue
                # 变更信息
                if not self.get_change_info(session, change_url, change_data, data):
                    self.log.warn('变更信息抓取失败....pripid = {pripid}'.format(pripid=pri_pid))
                    continue
                # 获得年报信息
                # 6.5W家年报暂时满足要求 可暂缓
                # self.get_annual_info(session, base_text, data)
                # 股东信息
                if not self.get_shareholder_info(session, investor_url, investor_data, data):
                    self.log.warn('股东信息抓取失败....pripid = {pripid}'.format(pripid=pri_pid))
                    continue

                data_list.append(data)
            except Exception as e:
                self.log.exception(e)
        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
